<template>
  <div class="wrapper">
    <div style="height: 60px; line-height: 60px; font-size: 20px; padding-left: 50px; color: white;
      background-color: rgba(0,0,0,0.2)">管理系统
    </div>

    <div style="margin: 100px auto; background: white; width: 500px; border-radius: 10px; overflow: hidden">
      <el-tabs type="card" v-model="activeName">

        <el-tab-pane label="账号密码登录" name="first">
          <div
              style="margin: 20px auto; background-color: #fff; width: 350px; height: 260px; padding: 20px; border-radius: 10px">
            <el-form :model="user" :rules="rules" ref="userForm">
              <el-form-item prop="username">
                <el-input size="medium" prefix-icon="el-icon-user" v-model="user.username"></el-input>
              </el-form-item>
              <el-form-item prop="password">
                <el-input size="medium" prefix-icon="el-icon-lock" show-password v-model="user.password"></el-input>
              </el-form-item>
              <el-form-item>
                <div style="display: flex">
                  <el-input size="medium" v-model="user.code" style="width: 200px"  placeholder="验证码"></el-input>
               <el-image style="width: 200px; margin-left: 5px; height: 35px; cursor:pointer"  :src="imgcode" alt="" @click="getCode" ></el-image>
                </div>
              </el-form-item>

              <el-form-item style="text-align: right">
                <el-button type="warning" size="medium" autocomplete="off" @click="$router.push('/register')">前往注册
                </el-button>
                <el-button type="primary" size="medium" autocomplete="off" @click="loginAccount">登录</el-button>

              </el-form-item>
              <el-form-item style="margin: 5px 0; text-align: right">
                <el-button type="text" size="medium" autocomplete="off" @click="handlePass">找回密码</el-button>
              </el-form-item>
            </el-form>
          </div>
        </el-tab-pane>

        <el-tab-pane label="邮箱登录" name="second">
          <div
              style="margin: 20px auto; background-color: #fff; width: 350px; height: 200px; padding: 20px; border-radius: 10px">
            <el-form :model="user" :rules="rules" ref="userForm">
              <el-form-item prop="email">
                <el-input size="medium" prefix-icon="el-icon-message" v-model="user.email"></el-input>
              </el-form-item>
              <el-form-item prop="code">
                <el-input size="medium" prefix-icon="el-icon-lock" style="width: 190px" v-model="user.code"></el-input>
                <el-button  type="primary" size="medium" class="ml-5" @click="sendEmailCode(1)">获取验证码</el-button>
              </el-form-item>
              <el-form-item style="margin: 10px 0; text-align: right">
                <el-button type="warning" size="medium" autocomplete="off" @click="$router.push('/register')">前往注册
                </el-button>
                <el-button type="primary" size="medium" autocomplete="off" @click="loginEmail">登录</el-button>
              </el-form-item>
              <el-form-item style="margin: 10px 0; text-align: right">
                <el-button  type="text" size="mid" autocomplete="off" @click="handlePass">找回密码</el-button>
              </el-form-item>
            </el-form>
          </div>
        </el-tab-pane>
      </el-tabs>
    </div>


    <el-dialog title="找回密码" :visible.sync="dialogFormVisible" width="30%">
      <el-form :model="pass" label-width="100px" size="small">
        <el-form-item label="邮箱">
          <el-input size="medium" v-model="pass.email" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="验证码">
          <el-input size="medium" style="width:100%" v-model="pass.code"></el-input>
          <el-button type="primary" size="medium" style="margin-top: 10px" @click="sendEmailCode(2)">获取验证码</el-button>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="passwordBack">重置密码</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {resetRouter, setRoutes} from "@/router";
// import Identify from "@/components/Identify";

export default {
  name: "Login",
  data() {
    return {
      fit:'contain',
      imgcode:'',
      activeName: 'first',
      user: {},
      pass: {
        email:'',
        code:''
      },
      code: '',
      dialogFormVisible: false,
      rules: {
        username: [
          {required: true, message: '请输入用户名', trigger: 'blur'},
          {min: 1, max: 20, message: '长度在 20个字符以内', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'},
          {min: 1, max: 20, message: '长度在 20个字符以内', trigger: 'blur'}
        ]
      }
    }
  },
  // components: {Identify},
  mounted() {
    // 重置路由
    resetRouter()
    this.getCode()
  },

  methods: {
    getCode(){
      this.request.get("/vc.png").then(res=>{
        this.imgcode = 'data:image/jpeg;base64,' + res.data
      }) },
    sendEmailCode(type) {
      let email;
      if (type === 1) {
        email = this.user.email
      } else if (type === 2) {
        email = this.pass.email
      }
      if (!email) {
        this.$message.warning("请输入邮箱账号")
        return
      }
      if (!/^\w+((.\w+)|(-\w+))@[A-Za-z0-9]+((.|-)[A-Za-z0-9]+).[A-Za-z0-9]+$/.test(email)) {
        this.$message.warning("请输入正确的邮箱账号")
        return
      }
      // 发送邮箱验证码
      this.request.get("/sys/email/" + email + "/" + type).then(res => {
        if (res.code === 200) {
          this.$message.success("发送成功")
        } else {
          this.$message.error(res.message)
        }
      })
    },
    loginAccount() {
      this.$refs['userForm'].validate((valid) => {
        if (valid) {// 表单校验合法
          if (!this.user.username){
            this.$message.error("请输入用户名")
            return
          }
          if (!this.user.password){
            this.$message.error("请输入密码")
            return
          }
          if (!this.user.code){
            this.$message.error("请输入验证码")
            return
          }
          this.request.post("/sys/loginAccount", this.user).then(res => {
            if (res.code === 200) {
              localStorage.setItem("user", JSON.stringify(res.data))  // 存储用户信息到浏览器
              localStorage.setItem("menus", JSON.stringify(res.data.menus))  // 存储用户信息到浏览器

              // 动态设置当前用户的路由
              setRoutes()
              this.$router.push("/")
              this.$message.success("登录成功")
            } else {
              this.$message.error(res.message)
            }
          })
        }
      });
    },
    loginEmail() {
      if (!this.user.email) {
        this.$message.warning("请输入邮箱")
        return
      }
      if (!this.user.code) {
        this.$message.warning("请输入验证码")
        return
      }
      this.request.post("/sys/loginEmail", this.user).then(res => {
        if (res.code === 200) {
          localStorage.setItem("user", JSON.stringify(res.data))  // 存储用户信息到浏览器
          localStorage.setItem("menus", JSON.stringify(res.data.menus))  // 存储用户信息到浏览器

          // 动态设置当前用户的路由
          setRoutes()
          this.$router.push("/")
          this.$message.success("登录成功")
        } else {
          this.$message.error(res.message)
        }
      })
    },
    handlePass() {
      this.dialogFormVisible = true
      this.pass = {}
    },
    passwordBack() {
      this.request.put("/sys/reset", this.pass).then(res => {
        if (res.code === 200) {
          this.$message.success("重置密码成功，新密码为：123，请尽快修改密码")
          this.dialogFormVisible = false
        } else {
           this.$message.error(res.message)
        }
      })
    }
  }
}
</script>

<style>
.wrapper {
  height: 100vh;
  background-image: linear-gradient(to bottom right, #FC466B, #3F5EFB);
  overflow: hidden;
}
.ml-4{
  margin-left: 5px;
  margin-top: 10px;
}
</style>